//
//  menuScrollView.swift
//  tryMenuScrollView
//
//  Created by ning on 2017/11/8.
//  Copyright © 2017年 ning. All rights reserved.
//

import UIKit

class menuScrollView: UIView {

    var titleScrollView: UIScrollView = UIScrollView()
    
    var contentScrollView: UIScrollView = UIScrollView()
    
    let lineView = UIView()
    
    var titleHeight = 50
    var titleArray:[String] = Array()
    var titleCount:Int = 0
    var titleButtonWidth = 0
    var titleButtonTextColor = UIColor.black
    var titleButtonSelectedTextColor = UIColor.red
    var titleButtonTextFont = UIFont.systemFont(ofSize: 14)
    var selectedButtonIndex = 100
    
    var selectLineColor = UIColor.red
    
    
    var ClickSelectedButtonActionBlock:((_ button:UIButton)->())?
    
    
    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
 
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
  */
    
    override init(frame: CGRect) {
        super.init(frame: frame)
//        self.titleCount = titleArray.count
//        titleButtonWidth = Int(self.frame.width)/titleCount
//        setupBackgroundScrollView()
    }
    
    func initTitle(titleArr:Array<String>) {
        self.titleArray=titleArr
        self.titleCount = titleArray.count
        titleButtonWidth = Int(self.frame.width)/titleCount
        if titleArray.count>6 {
             titleButtonWidth = Int(self.frame.width)/6
        }else{
             titleButtonWidth = Int(self.frame.width)/titleCount
        }
        setupBackgroundScrollView()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
extension menuScrollView:UIScrollViewDelegate{
    func setupBackgroundScrollView(){
        titleScrollView.frame=CGRect(x:0,y:0,width:Int(self.frame.width),height:Int(titleHeight))
        self.addSubview(titleScrollView)
    titleScrollView.contentSize=CGSize(width:titleButtonWidth*titleCount,height:Int(titleScrollView.frame.height))
        titleScrollView.showsHorizontalScrollIndicator=false
        
        
        
        contentScrollView.frame=CGRect(x:0,y:Int(titleHeight),width:Int(self.frame.width),height:Int(self.frame.height)-titleHeight)
        contentScrollView.delegate=self
        self.addSubview(contentScrollView)
    contentScrollView.contentSize=CGSize(width:self.frame.width*CGFloat(titleCount),height:contentScrollView.frame.height)
        contentScrollView.showsHorizontalScrollIndicator=false
        
        setupTitleView()
        setupContentView()
        
    }
    func setupTitleView(){
        for i in 0...titleCount-1 {
            let titleButton = UIButton()
            titleButton.frame=CGRect(x:0+i*titleButtonWidth,y:0,width:Int(titleButtonWidth),height:titleHeight-1)
            titleButton.setTitle(titleArray[i], for: UIControlState.normal)
            titleButton.setTitleColor(titleButtonTextColor, for: UIControlState.normal)
            titleButton.titleLabel?.font=titleButtonTextFont
            titleButton.setTitleColor(titleButtonSelectedTextColor, for: UIControlState.selected)
            titleButton.addTarget(self, action: #selector(didTitleButtonTouchUpInside), for: UIControlEvents.touchUpInside)
            titleButton.tag=100+i
            titleScrollView.addSubview(titleButton)
        }
        lineView.frame=CGRect(x:0,y:titleHeight-1,width:Int(titleButtonWidth),height:1)
        lineView.backgroundColor=selectLineColor
        titleScrollView.addSubview(lineView)
    
    }
    
    func setupContentView(){
        for i in 0...titleCount-1 {
            let view = UIView()
            view.frame=CGRect(x:Int(contentScrollView.frame.width)*i,y:0,width:Int(contentScrollView.frame.width),height:Int(contentScrollView.frame.height))
            view.backgroundColor=UIColor.yellow
            contentScrollView.addSubview(view)
            let label = UILabel()
            label.frame=CGRect(x:0,y:0,width:view.frame.width,height:view.frame.height)
            label.text=titleArray[i]
            label.textAlignment=NSTextAlignment.center
            view.addSubview(label)
            if i == 0{
                let recommandView = Bundle.main.loadNibNamed("RecommandView", owner: nil, options: nil)?.last as! RecommandView
                recommandView.frame=CGRect(x:0,y:0,width:view.frame.width,height:view.frame.height)
                view.addSubview(recommandView)
                
            }else if(i == 1){
                let hotDotView = Bundle.main.loadNibNamed("HotDotView", owner: nil, options: nil)?.last as! HotDotView
                hotDotView.frame=CGRect(x:0,y:0,width:view.frame.width,height:view.frame.height)
                view.addSubview(hotDotView)
            }
        }
        
    }
    
}
extension menuScrollView{
    
    @objc func didTitleButtonTouchUpInside(button:UIButton){
        
        selectedButtonIndex = button.tag
        changeSelectedButton()
        changeLinePosition(index:selectedButtonIndex-100)
        changeScrollViewPosition(index:selectedButtonIndex-100)
        ClickSelectedButtonActionBlock?(button)
    }
    
    func changeSelectedButton(){
        for i in 0...titleCount-1 {
            let button:UIButton = self.viewWithTag(100+i) as! UIButton
            button.isSelected=false
        }
        let selectedButton:UIButton = self.viewWithTag(selectedButtonIndex) as! UIButton
        selectedButton.isSelected=true
    }
    
    func changeLinePosition(index:NSInteger){
        UIView.animate(withDuration: 0.3) {
            self.lineView.frame=CGRect(x:0+index*self.titleButtonWidth,y:self.titleHeight-1,width:Int(self.titleButtonWidth),height:1)
        }
    }
    
    func changeScrollViewPosition(index:NSInteger){
        var offSet:CGPoint = contentScrollView.contentOffset
        offSet.x = contentScrollView.frame.width * CGFloat(index)
        UIView.animate(withDuration: 0.3) {
            self.contentScrollView.contentOffset=offSet
        }
    }
 
}

//MARK:UIScrollViewDelegate
extension menuScrollView{
   
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        let offSetX = scrollView.contentOffset.x
        selectedButtonIndex=Int(offSetX/self.frame.width)+100
        changeSelectedButton()
        changeLinePosition(index:selectedButtonIndex-100)
        changeScrollViewPosition(index:selectedButtonIndex-100)
        let selectedButton:UIButton = self.viewWithTag(selectedButtonIndex) as! UIButton
        ClickSelectedButtonActionBlock?(selectedButton)
    }
   
}
